import { Request, RunWaitQueue, getUrl } from '@/common/request'
import { saveToken, getToken, getWxAppId, saveUserInfo, getOpenid } from '@/common/util'

export const successCode = "000000"
export const successCode2 = 3000
export const successCodeForWeizhi = 1

export const JSMRequest = (params, code = successCode2) => {
	return new Promise((resolve, reject) => {
		Request({
			tag: params.tag,
			method: params.method || 'GET',
			params: params.params || {},
			success: res => {
				if(res.code == code){
					resolve(res)
				}else{
					reject(res)
				}
			},
			fail: () => {
				reject({
					msg: "系统繁忙，请稍后再试"
				})
			}
		})
	})
}

export const RunRequest = (params, code) => {
	code = code || successCode
    Request({
        tag: params.tag,
        method: params.method || 'GET',
        params: params.params || {},
        success: res => {
            if(res.code == code){
                params.success && params.success(res)
            }else{
                params.fail && params.fail(res)
            }
        },
        fail: params.error
    })
}

// 登录
export const runLogin = params => {
    Request({
        tag: 'login_wx',
        method: 'POST',
        params: params.params || {},
        success: res => {
            if(res.code == successCodeForWeizhi){
                params.success && params.success(res)
            }else{
                params.fail && params.fail(res)
            }
        },
        fail: params.error
    })
}

// 获取用户信息
export const runGetUserInfo = params => {
    Request({
        tag: 'fanli_user',
        method: params.method || 'GET',
        params: params.params || {},
        success: res => {
            if(res.code == successCode2){
                params.success && params.success(res)
            }else{
                params.fail && params.fail(res)
            }
        },
        fail: params.error
    })
}

// 获取广告位信息
export const runGetAdData = params => {
    Request({
        tag: 'home_ad',
        method: params.method || 'GET',
        params: params.params || {},
        success: res => {
            if(res.code == successCodeForWeizhi){
                params.success && params.success(res)
            }else{
                params.fail && params.fail(res)
            }
        },
        fail: params.error
    })
}

// 获取商圈城市数据
export const runGetCityList = params => {
    params.tag = 'city_list'
	
	RunRequest(params)
}

// 获取地区数据
export const runGetCityListBy = params => {
    params.tag = 'fanli_user'
	params.params = params.params || {}
	params.params.act = "gaincity"
	
	RunRequest(params, successCode2)
}

// 获取商圈商品列表
export const runGetSShopGoodList = (params) =>{
    params.tag = 'oem_sshop_good_list'
	
    RunRequest(params)
}

// 获取商圈商品列表
export const runGetSupplierCate = (params) =>{
    params.tag = 'supplier_cate'
	
    RunRequest(params)
}

// 获取商圈商家列表
export const runGetSupplierList = (params) =>{
    params.tag = 'supplier_list'
	
    RunRequest(params)
}

// 获取商圈商家列表
export const runGetSupplierListWithGoods = (params) =>{
    params.tag = 'supplier_list_has_goods'
	
    RunRequest(params)
}

// 获取商圈商家详情
export const runGetSupplierDetail = (params) =>{
    params.tag = 'supplier_detail'
	
    RunRequest(params)
}

// 获取商圈商家商品详情
export const runGetSShopGoodDetail = (params) =>{
    params.tag = 'oem_sshop_good_detail'
	
    RunRequest(params)
}

// 获取优惠商城v6数据
export const runGetV6Data = (params) =>{
    let tag = ''
    switch (params.platform) {
        case 'jd' :
            tag = 'v6-jd'; break;
        case 'tb' :
            tag = 'v6-tb'; break;
        case 'pdd' :
            tag = 'v6-pdd'; break;
        default: 
            tag = 'v6-index'
    }
	params.tag = tag
    RunRequest(params)
}

// 获取优惠商城热搜关键词
export const runGetSearchHotTag = (params) =>{
	params.tag = "search_hot_keyword"
    RunRequest(params)
}

// 我的订单
export const runGetOrderList = (params) =>{
	params.tag = "fanli_my_order_list"
	
    RunRequest(params)
}

// 我的订单 自营
export const runSelfShopOrderList = (params) =>{
	params.tag = "oem_sshop_order_list"
	
    RunRequest(params)
}

// 我的订单 自营 申请退款
export const runRefundSelfSupportOrder = (params) => {
	params.tag = "oem_sshop_order_refund"
	
	RunRequest(params)
}

// 我的订单 自营 确认收货
export const runSelfSupportOrderReceiving = (params) => {
	params.tag = "oem_sshop_order_confirm"
	
	RunRequest(params)
}

// 我的订单 自营 取消订单
export const runSelfSupportOrderDelete = (params) => {
	params.tag = "oem_sshop_order_delete"
	
	RunRequest(params)
}

// 我的地址
export const runGetAddressList = (params) => {
	params.tag = "fanli_user"
	params.params = params.params || {}
	params.params.act = "queryuseraddress"
	
	RunRequest(params, successCode2)
}

// 我的地址 删除
export const runDeleteAddress = (params) => {
	params.tag = "fanli_user"
	params.params = params.params || {}
	params.params.act = "deleteaddress"
	
	RunRequest(params, successCode2)
}

// 我的地址 保存
export const runSaveAddress = (params) => {
	params.tag = "fanli_user"
	params.params = params.params || {}
	params.params.act = "keepuseraddress"
	params.method = "POST"
	
	RunRequest(params, successCode2)
}

// 提现
export const runWithdrawal = (params) => {
	params.tag = "fanli_vip_money"
	params.method = "POST"
	params.params = params.params || {}
	params.params.act = "cashlog"
	
	RunRequest(params, successCode2)
}

// 提现
export const runWithdrawalList = (params) => {
	params.tag = "fanli_vip_money"
	params.params = params.params || {}
	params.params.act = "cashloglist"
	
	RunRequest(params, successCode2)
}

// 支付宝
export const runUserAlipayInfo = (params) => {
	params.tag = "fanli_user"
	params.params = params.params || {}
	params.params.act = "alipayuser"
	
	RunRequest(params, successCode2)
}

// 短信验证码
export const runGetSmsCode = (params) => {
	params.tag = "sms_code"
	
	RunRequest(params, successCodeForWeizhi)
}

// 收益明细
export const runMyProfitData = (params) => {
	params.tag = "fanli_vip_money"
	params.params = params.params || {}
	params.params.act = "olist"
	
	RunRequest(params, successCode2)
}

// 升级参数
export const runGetLevelupData = (params) => {
	params.tag = "fanli_vip"
	params.params = params.params || {}
	params.params.act = "riselist"
	
	RunRequest(params, successCode2)
}

// 获取礼品券数据
export const runMyGiftData = (params) => {
	params.tag = "fanli_gift_list"
	
	RunRequest(params, successCode2)
}

// 视频卡列表
export const runGetVideoCard = (params) => {
	params.tag = "video_card_list"
	
	RunRequest(params, successCode2)
}

// 充值视频卡
export const runRechargeVideoCard = (params) => {
	params.tag = "submit_video_card"
	
	RunRequest(params, successCode2)
}

// 创建支付
export const runSubmitOrder = (params) => {
	params.tag = "fanli_vip"
	params.params = params.params || {}
	params.params.act = "pay"
	params.params.type_pay = 1
	params.params.type_class = "jsapi"
	
	RunRequest(params, successCode2)
}

// 创建升级订单
export const runCreateLevelupOrder = (params) => {
	params.tag = "fanli_vip"
	params.params = params.params || {}
	params.params.act = "addorder"
	params.params.openid = getOpenid()
	
	RunRequest(params, successCode2)
}

// 爆品
export const runGetCommunity = (params) => {
	params.tag = "fanli_community"
	params.params = params.params || {}
	params.params.type = 1
	
	RunRequest(params, successCode)
}

// 爆品分享
export const runGetCommunityShare = (params) => {
	params.tag = "fanli_community_share"
	params.params = params.params || {}
	params.params.platfrom_type = "wx_mini"
	
	RunRequest(params, successCode)
}

// 邀请好友
export const runGetInvite = (params) => {
	params.tag = "fanli_user"
	params.params = params.params || {}
	params.params.act = "inviteimg"
	params.params.small_app = 1
	
	RunRequest(params, successCode2)
}

// 小程序吗
export const runGetWxCode = (params) => {
	params.tag = "wx-code"
	
	RunRequest(params, successCode2)
}